﻿using Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Data;

namespace Common.UI.Converters
{
	public class DateTimeToStringConverter : IValueConverter
	{
		public String Format { get; set; }
		public String Default { get; set; }

		public DateTimeToStringConverter()
		{
			Format = "d MMM";
		}

		public object Convert(object value, Type targetType, object parameter, string language)
		{
			var date = (DateTime)value;

			if (date.Ticks < 1)
				return Default;

            var delta = DateTimeHelper.Now - date;

            if (delta.TotalMinutes < 15)
                return "a moment ago";
            else
            {
                var h = (Int32)Math.Floor(delta.TotalHours);
                if (h < 6 && h > 0)
                    return String.Format("{0} hours ago", h);
            }

			if (date.Date == DateTimeHelper.Now.Date)
				return "TODAY";
			else if (date.Date == DateTimeHelper.Yesterday.Date)
				return "YESTERDAY";
			else if (date.Date == DateTimeHelper.Now.AddDays(-7).Date)
				return "A WEEK AGO";

			var diff = (DateTimeHelper.Now.Date - date.Date);
			var days = String.Format("{0} days ago", diff.TotalDays);
			if (diff.TotalDays < 7)
				return days;

			if (String.IsNullOrWhiteSpace(Format))
				return days;
			return String.Format("{0}, {1}", date.ToString(Format), days);
		}

		public object ConvertBack(object value, Type targetType, object parameter, string language)
		{
			throw new NotImplementedException();
		}
	}
}
